package ctn.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ctn.contant.WebConstant;
import ctn.convert.CTNUserConvert;
import ctn.hibernate.po.CtnUser;
import ctn.service.dto.CTNUserDTO;
import ctn.util.CTNUserUtil;

public class CtnUserFilter implements Filter {

	@Override
	public void destroy() {

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse rep,
			FilterChain arg2) throws IOException, ServletException {
		HttpSession session = (HttpSession) ((HttpServletRequest) req)
				.getSession();
		CtnUser user = (CtnUser) session.getAttribute(WebConstant.USER);
		if (user == null) {
			((HttpServletResponse) rep).sendRedirect(session
					.getServletContext().getContextPath()
					+ "/app/index");
			arg2.doFilter(req, rep);
			return;
		}
		if(CTNUserUtil.getUser()==null){
		
			CTNUserUtil.set(user);
		}
		arg2.doFilter(req, rep);

	}

}
